package com.bestcem.xm.ticket.controller;

import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.controller.v2.TicketFilterController;
import com.bestcem.xm.ticket.controller.vo.IdAndTitleVO;
import com.bestcem.xm.ticket.controller.vo.SurveyAndRuleVO;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.service.FilterService;
import com.bestcem.xm.ticket.service.TicketConfigService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 工单规则过滤接口
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/31 10:35
 * @deprecated {@link TicketFilterController}
 */
@Deprecated
@Api(tags = "工单规则过滤接口")
@RestController
@RequestMapping(value = "/ticket/tickets/rule", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketsRuleFilterController {

    @Resource
    private TicketConfigDao ticketConfigDao;
    @Resource
    private TicketConfigService ticketConfigService;
    @Resource
    private FilterService filterService;
    @Resource
    private HttpServletRequest request;

    /**
     * 分页获取生成规则筛选条件选项
     *
     * @param page 页号
     * @param size 大小
     * @return 分页DTO
     * @author @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/18 21:52
     * @deprecated {@link TicketFilterController#ruleFilter}
     */
    @Deprecated
    @ApiOperation(value = "/rule/filter/")
    @GetMapping(value = {"/filter/{configId}", "/filter/"})
    public XmResultJson<Object> ruleFilter(@PathVariable(required = false) String configId,
                                           @RequestParam(value = "page", required = false) Integer page,
                                           @RequestParam(value = "rowsPerPage", required = false) Integer size) {

        String orgId = SecurityContextHolder.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id格式错误");
        }
        // 当前端传了TicketConfigId时,直接查表返回即可
        if (StrUtil.isNotEmpty(configId)) {
            if (ObjectId.isValid(configId)) {
                TicketConfig ticketConfig = ticketConfigDao.findById(configId);
                if (Objects.nonNull(ticketConfig)) {
                    return XmResultJson.success(
                            new IdAndTitleVO(ticketConfig.getTitle(), ticketConfig.getId()));

                }
                return XmResultJson.success(new SurveyAndRuleVO.AlertRule());
            }
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "TicketConfig的id格式错误");
        }

        // 当前端没有传TicketConfigId时,需要校验
        if (Objects.isNull(page)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少page参数");
        }
        if (Objects.isNull(size)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少rowsPerPage参数");
        }
        if (page <= 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "page不在范围内");
        }
        if (size <= 0 || size >= 999) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "rowsPerPage不在范围内");
        }

        // 校验完成,构建分页参数
        PageParam pageParam = new PageParam(page, size);
        ServiceResult<PageDTO<IdAndTitleVO>> result = filterService.ruleFilter(orgId, pageParam);
        if (result.isSuccess()) {
            return XmResultJson.success(result.getData());
        }
        return XmResultJson.fail(result.getErrorData(), result.getMsg());
    }
}
